{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split, GridSearchCV\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.neighbors import KNeighborsClassifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.获取数据集\n",
    "# 2.基本数据处理\n",
    "# 2.1 缩小数据范围\n",
    "# 2.2 选择时间特征\n",
    "# 2.3 去掉签到较少的地方\n",
    "# 2.4 确定特征值和目标值\n",
    "# 2.5 分割数据集\n",
    "# 3.特征工程 -- 特征预处理(标准化)\n",
    "# 4.机器学习 -- knn+cv\n",
    "# 5.模型评估"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.获取数据集\n",
    "data = pd.read_csv(\"./data/FBlocation/train.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>time</th>\n",
       "      <th>place_id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0.7941</td>\n",
       "      <td>9.0809</td>\n",
       "      <td>54</td>\n",
       "      <td>470702</td>\n",
       "      <td>8523065625</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>5.9567</td>\n",
       "      <td>4.7968</td>\n",
       "      <td>13</td>\n",
       "      <td>186555</td>\n",
       "      <td>1757726713</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>8.3078</td>\n",
       "      <td>7.0407</td>\n",
       "      <td>74</td>\n",
       "      <td>322648</td>\n",
       "      <td>1137537235</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>7.3665</td>\n",
       "      <td>2.5165</td>\n",
       "      <td>65</td>\n",
       "      <td>704587</td>\n",
       "      <td>6567393236</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>4.0961</td>\n",
       "      <td>1.1307</td>\n",
       "      <td>31</td>\n",
       "      <td>472130</td>\n",
       "      <td>7440663949</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   row_id       x       y  accuracy    time    place_id\n",
       "0       0  0.7941  9.0809        54  470702  8523065625\n",
       "1       1  5.9567  4.7968        13  186555  1757726713\n",
       "2       2  8.3078  7.0407        74  322648  1137537235\n",
       "3       3  7.3665  2.5165        65  704587  6567393236\n",
       "4       4  4.0961  1.1307        31  472130  7440663949"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>time</th>\n",
       "      <th>place_id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>2.911802e+07</td>\n",
       "      <td>2.911802e+07</td>\n",
       "      <td>2.911802e+07</td>\n",
       "      <td>2.911802e+07</td>\n",
       "      <td>2.911802e+07</td>\n",
       "      <td>2.911802e+07</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>1.455901e+07</td>\n",
       "      <td>4.999770e+00</td>\n",
       "      <td>5.001814e+00</td>\n",
       "      <td>8.284912e+01</td>\n",
       "      <td>4.170104e+05</td>\n",
       "      <td>5.493787e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>8.405649e+06</td>\n",
       "      <td>2.857601e+00</td>\n",
       "      <td>2.887505e+00</td>\n",
       "      <td>1.147518e+02</td>\n",
       "      <td>2.311761e+05</td>\n",
       "      <td>2.611088e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>1.000000e+00</td>\n",
       "      <td>1.000016e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>7.279505e+06</td>\n",
       "      <td>2.534700e+00</td>\n",
       "      <td>2.496700e+00</td>\n",
       "      <td>2.700000e+01</td>\n",
       "      <td>2.030570e+05</td>\n",
       "      <td>3.222911e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>1.455901e+07</td>\n",
       "      <td>5.009100e+00</td>\n",
       "      <td>4.988300e+00</td>\n",
       "      <td>6.200000e+01</td>\n",
       "      <td>4.339220e+05</td>\n",
       "      <td>5.518573e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>2.183852e+07</td>\n",
       "      <td>7.461400e+00</td>\n",
       "      <td>7.510300e+00</td>\n",
       "      <td>7.500000e+01</td>\n",
       "      <td>6.204910e+05</td>\n",
       "      <td>7.764307e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>2.911802e+07</td>\n",
       "      <td>1.000000e+01</td>\n",
       "      <td>1.000000e+01</td>\n",
       "      <td>1.033000e+03</td>\n",
       "      <td>7.862390e+05</td>\n",
       "      <td>9.999932e+09</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             row_id             x             y      accuracy          time  \\\n",
       "count  2.911802e+07  2.911802e+07  2.911802e+07  2.911802e+07  2.911802e+07   \n",
       "mean   1.455901e+07  4.999770e+00  5.001814e+00  8.284912e+01  4.170104e+05   \n",
       "std    8.405649e+06  2.857601e+00  2.887505e+00  1.147518e+02  2.311761e+05   \n",
       "min    0.000000e+00  0.000000e+00  0.000000e+00  1.000000e+00  1.000000e+00   \n",
       "25%    7.279505e+06  2.534700e+00  2.496700e+00  2.700000e+01  2.030570e+05   \n",
       "50%    1.455901e+07  5.009100e+00  4.988300e+00  6.200000e+01  4.339220e+05   \n",
       "75%    2.183852e+07  7.461400e+00  7.510300e+00  7.500000e+01  6.204910e+05   \n",
       "max    2.911802e+07  1.000000e+01  1.000000e+01  1.033000e+03  7.862390e+05   \n",
       "\n",
       "           place_id  \n",
       "count  2.911802e+07  \n",
       "mean   5.493787e+09  \n",
       "std    2.611088e+09  \n",
       "min    1.000016e+09  \n",
       "25%    3.222911e+09  \n",
       "50%    5.518573e+09  \n",
       "75%    7.764307e+09  \n",
       "max    9.999932e+09  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(29118021, 6)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2.基本数据处理\n",
    "# 2.1 缩小数据范围\n",
    "partial_data = data.query(\"x>2.0 & x<2.5 & y>2.0 & y<2.5\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>time</th>\n",
       "      <th>place_id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>163</th>\n",
       "      <td>163</td>\n",
       "      <td>2.1663</td>\n",
       "      <td>2.3755</td>\n",
       "      <td>84</td>\n",
       "      <td>669737</td>\n",
       "      <td>3869813743</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>310</th>\n",
       "      <td>310</td>\n",
       "      <td>2.3695</td>\n",
       "      <td>2.2034</td>\n",
       "      <td>3</td>\n",
       "      <td>234719</td>\n",
       "      <td>2636621520</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>658</th>\n",
       "      <td>658</td>\n",
       "      <td>2.3236</td>\n",
       "      <td>2.1768</td>\n",
       "      <td>66</td>\n",
       "      <td>502343</td>\n",
       "      <td>7877745055</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1368</th>\n",
       "      <td>1368</td>\n",
       "      <td>2.2613</td>\n",
       "      <td>2.3392</td>\n",
       "      <td>73</td>\n",
       "      <td>319822</td>\n",
       "      <td>9775192577</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1627</th>\n",
       "      <td>1627</td>\n",
       "      <td>2.3331</td>\n",
       "      <td>2.0011</td>\n",
       "      <td>66</td>\n",
       "      <td>595084</td>\n",
       "      <td>6731326909</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      row_id       x       y  accuracy    time    place_id\n",
       "163      163  2.1663  2.3755        84  669737  3869813743\n",
       "310      310  2.3695  2.2034         3  234719  2636621520\n",
       "658      658  2.3236  2.1768        66  502343  7877745055\n",
       "1368    1368  2.2613  2.3392        73  319822  9775192577\n",
       "1627    1627  2.3331  2.0011        66  595084  6731326909"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "partial_data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(71664, 6)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "partial_data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "163     669737\n",
       "310     234719\n",
       "658     502343\n",
       "1368    319822\n",
       "1627    595084\n",
       "Name: time, dtype: int64"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2.2 选择时间特征\n",
    "partial_data[\"time\"].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "163    1970-01-08 18:02:17\n",
       "310    1970-01-03 17:11:59\n",
       "658    1970-01-06 19:32:23\n",
       "1368   1970-01-04 16:50:22\n",
       "1627   1970-01-07 21:18:04\n",
       "Name: time, dtype: datetime64[ns]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time = pd.to_datetime(partial_data[\"time\"], unit=\"s\")\n",
    "# 脱敏\n",
    "time.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['1970-01-08 18:02:17', '1970-01-03 17:11:59',\n",
       "               '1970-01-06 19:32:23', '1970-01-04 16:50:22',\n",
       "               '1970-01-07 21:18:04', '1970-01-02 03:14:59',\n",
       "               '1970-01-07 03:45:16', '1970-01-05 03:28:43',\n",
       "               '1970-01-01 18:59:03', '1970-01-09 07:50:12',\n",
       "               ...\n",
       "               '1970-01-09 20:03:34', '1970-01-08 09:26:50',\n",
       "               '1970-01-07 04:45:59', '1970-01-07 22:36:18',\n",
       "               '1970-01-06 23:29:43', '1970-01-03 12:31:26',\n",
       "               '1970-01-04 15:19:20', '1970-01-01 20:49:14',\n",
       "               '1970-01-03 09:17:37', '1970-01-02 20:34:43'],\n",
       "              dtype='datetime64[ns]', name='time', length=71664, freq=None)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time = pd.DatetimeIndex(time)\n",
    "time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Int64Index([18, 17, 19, 16, 21,  3,  3,  3, 18,  7,\n",
       "            ...\n",
       "            20,  9,  4, 22, 23, 12, 15, 20,  9, 20],\n",
       "           dtype='int64', name='time', length=71664)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.hour"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Int64Index([8, 3, 6, 4, 7, 2, 7, 5, 1, 9,\n",
       "            ...\n",
       "            9, 8, 7, 7, 6, 3, 4, 1, 3, 2],\n",
       "           dtype='int64', name='time', length=71664)"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.day"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/sherwin/workspaces/ai/lib/python3.6/site-packages/ipykernel_launcher.py:1: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n",
      "  \"\"\"Entry point for launching an IPython kernel.\n",
      "/Users/sherwin/workspaces/ai/lib/python3.6/site-packages/ipykernel_launcher.py:2: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n",
      "  \n",
      "/Users/sherwin/workspaces/ai/lib/python3.6/site-packages/ipykernel_launcher.py:3: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n",
      "  This is separate from the ipykernel package so we can avoid doing imports until\n"
     ]
    }
   ],
   "source": [
    "partial_data[\"hour\"] = time.hour\n",
    "partial_data[\"day\"] = time.day\n",
    "partial_data[\"weekday\"] = time.weekday"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>time</th>\n",
       "      <th>place_id</th>\n",
       "      <th>hour</th>\n",
       "      <th>day</th>\n",
       "      <th>weekday</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>163</th>\n",
       "      <td>163</td>\n",
       "      <td>2.1663</td>\n",
       "      <td>2.3755</td>\n",
       "      <td>84</td>\n",
       "      <td>669737</td>\n",
       "      <td>3869813743</td>\n",
       "      <td>18</td>\n",
       "      <td>8</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>310</th>\n",
       "      <td>310</td>\n",
       "      <td>2.3695</td>\n",
       "      <td>2.2034</td>\n",
       "      <td>3</td>\n",
       "      <td>234719</td>\n",
       "      <td>2636621520</td>\n",
       "      <td>17</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>658</th>\n",
       "      <td>658</td>\n",
       "      <td>2.3236</td>\n",
       "      <td>2.1768</td>\n",
       "      <td>66</td>\n",
       "      <td>502343</td>\n",
       "      <td>7877745055</td>\n",
       "      <td>19</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1368</th>\n",
       "      <td>1368</td>\n",
       "      <td>2.2613</td>\n",
       "      <td>2.3392</td>\n",
       "      <td>73</td>\n",
       "      <td>319822</td>\n",
       "      <td>9775192577</td>\n",
       "      <td>16</td>\n",
       "      <td>4</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1627</th>\n",
       "      <td>1627</td>\n",
       "      <td>2.3331</td>\n",
       "      <td>2.0011</td>\n",
       "      <td>66</td>\n",
       "      <td>595084</td>\n",
       "      <td>6731326909</td>\n",
       "      <td>21</td>\n",
       "      <td>7</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      row_id       x       y  accuracy    time    place_id  hour  day  weekday\n",
       "163      163  2.1663  2.3755        84  669737  3869813743    18    8        3\n",
       "310      310  2.3695  2.2034         3  234719  2636621520    17    3        5\n",
       "658      658  2.3236  2.1768        66  502343  7877745055    19    6        1\n",
       "1368    1368  2.2613  2.3392        73  319822  9775192577    16    4        6\n",
       "1627    1627  2.3331  2.0011        66  595084  6731326909    21    7        2"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "partial_data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>time</th>\n",
       "      <th>place_id</th>\n",
       "      <th>hour</th>\n",
       "      <th>day</th>\n",
       "      <th>weekday</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>163</th>\n",
       "      <td>163</td>\n",
       "      <td>2.1663</td>\n",
       "      <td>2.3755</td>\n",
       "      <td>84</td>\n",
       "      <td>669737</td>\n",
       "      <td>3869813743</td>\n",
       "      <td>18</td>\n",
       "      <td>8</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>310</th>\n",
       "      <td>310</td>\n",
       "      <td>2.3695</td>\n",
       "      <td>2.2034</td>\n",
       "      <td>3</td>\n",
       "      <td>234719</td>\n",
       "      <td>2636621520</td>\n",
       "      <td>17</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>658</th>\n",
       "      <td>658</td>\n",
       "      <td>2.3236</td>\n",
       "      <td>2.1768</td>\n",
       "      <td>66</td>\n",
       "      <td>502343</td>\n",
       "      <td>7877745055</td>\n",
       "      <td>19</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1368</th>\n",
       "      <td>1368</td>\n",
       "      <td>2.2613</td>\n",
       "      <td>2.3392</td>\n",
       "      <td>73</td>\n",
       "      <td>319822</td>\n",
       "      <td>9775192577</td>\n",
       "      <td>16</td>\n",
       "      <td>4</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1627</th>\n",
       "      <td>1627</td>\n",
       "      <td>2.3331</td>\n",
       "      <td>2.0011</td>\n",
       "      <td>66</td>\n",
       "      <td>595084</td>\n",
       "      <td>6731326909</td>\n",
       "      <td>21</td>\n",
       "      <td>7</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      row_id       x       y  accuracy    time    place_id  hour  day  weekday\n",
       "163      163  2.1663  2.3755        84  669737  3869813743    18    8        3\n",
       "310      310  2.3695  2.2034         3  234719  2636621520    17    3        5\n",
       "658      658  2.3236  2.1768        66  502343  7877745055    19    6        1\n",
       "1368    1368  2.2613  2.3392        73  319822  9775192577    16    4        6\n",
       "1627    1627  2.3331  2.0011        66  595084  6731326909    21    7        2"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2.3 去掉签到较少的地方\n",
    "partial_data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "place_count = partial_data.groupby(\"place_id\").count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>time</th>\n",
       "      <th>hour</th>\n",
       "      <th>day</th>\n",
       "      <th>weekday</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>place_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1006234733</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1008823061</th>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1012580558</th>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1025585791</th>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1026507711</th>\n",
       "      <td>220</td>\n",
       "      <td>220</td>\n",
       "      <td>220</td>\n",
       "      <td>220</td>\n",
       "      <td>220</td>\n",
       "      <td>220</td>\n",
       "      <td>220</td>\n",
       "      <td>220</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            row_id    x    y  accuracy  time  hour  day  weekday\n",
       "place_id                                                        \n",
       "1006234733       1    1    1         1     1     1    1        1\n",
       "1008823061       4    4    4         4     4     4    4        4\n",
       "1012580558       3    3    3         3     3     3    3        3\n",
       "1025585791      21   21   21        21    21    21   21       21\n",
       "1026507711     220  220  220       220   220   220  220      220"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "place_count.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>time</th>\n",
       "      <th>hour</th>\n",
       "      <th>day</th>\n",
       "      <th>weekday</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>place_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1008823061</th>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1025585791</th>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1026507711</th>\n",
       "      <td>220</td>\n",
       "      <td>220</td>\n",
       "      <td>220</td>\n",
       "      <td>220</td>\n",
       "      <td>220</td>\n",
       "      <td>220</td>\n",
       "      <td>220</td>\n",
       "      <td>220</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1032417180</th>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1040557418</th>\n",
       "      <td>123</td>\n",
       "      <td>123</td>\n",
       "      <td>123</td>\n",
       "      <td>123</td>\n",
       "      <td>123</td>\n",
       "      <td>123</td>\n",
       "      <td>123</td>\n",
       "      <td>123</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            row_id    x    y  accuracy  time  hour  day  weekday\n",
       "place_id                                                        \n",
       "1008823061       4    4    4         4     4     4    4        4\n",
       "1025585791      21   21   21        21    21    21   21       21\n",
       "1026507711     220  220  220       220   220   220  220      220\n",
       "1032417180      10   10   10        10    10    10   10       10\n",
       "1040557418     123  123  123       123   123   123  123      123"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "place_count = place_count[place_count[\"row_id\"]>3]\n",
    "place_count.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "partial_data = partial_data[partial_data[\"place_id\"].isin(place_count.index)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(69264, 9)"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "partial_data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>row_id</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>time</th>\n",
       "      <th>place_id</th>\n",
       "      <th>hour</th>\n",
       "      <th>day</th>\n",
       "      <th>weekday</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>163</th>\n",
       "      <td>163</td>\n",
       "      <td>2.1663</td>\n",
       "      <td>2.3755</td>\n",
       "      <td>84</td>\n",
       "      <td>669737</td>\n",
       "      <td>3869813743</td>\n",
       "      <td>18</td>\n",
       "      <td>8</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>310</th>\n",
       "      <td>310</td>\n",
       "      <td>2.3695</td>\n",
       "      <td>2.2034</td>\n",
       "      <td>3</td>\n",
       "      <td>234719</td>\n",
       "      <td>2636621520</td>\n",
       "      <td>17</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>658</th>\n",
       "      <td>658</td>\n",
       "      <td>2.3236</td>\n",
       "      <td>2.1768</td>\n",
       "      <td>66</td>\n",
       "      <td>502343</td>\n",
       "      <td>7877745055</td>\n",
       "      <td>19</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1368</th>\n",
       "      <td>1368</td>\n",
       "      <td>2.2613</td>\n",
       "      <td>2.3392</td>\n",
       "      <td>73</td>\n",
       "      <td>319822</td>\n",
       "      <td>9775192577</td>\n",
       "      <td>16</td>\n",
       "      <td>4</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1627</th>\n",
       "      <td>1627</td>\n",
       "      <td>2.3331</td>\n",
       "      <td>2.0011</td>\n",
       "      <td>66</td>\n",
       "      <td>595084</td>\n",
       "      <td>6731326909</td>\n",
       "      <td>21</td>\n",
       "      <td>7</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      row_id       x       y  accuracy    time    place_id  hour  day  weekday\n",
       "163      163  2.1663  2.3755        84  669737  3869813743    18    8        3\n",
       "310      310  2.3695  2.2034         3  234719  2636621520    17    3        5\n",
       "658      658  2.3236  2.1768        66  502343  7877745055    19    6        1\n",
       "1368    1368  2.2613  2.3392        73  319822  9775192577    16    4        6\n",
       "1627    1627  2.3331  2.0011        66  595084  6731326909    21    7        2"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "partial_data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2.4 确定特征值和目标值\n",
    "x = partial_data[[\"x\", \"y\", \"accuracy\", \"hour\", \"day\", \"weekday\"]]\n",
    "y = partial_data[\"place_id\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>hour</th>\n",
       "      <th>day</th>\n",
       "      <th>weekday</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>163</th>\n",
       "      <td>2.1663</td>\n",
       "      <td>2.3755</td>\n",
       "      <td>84</td>\n",
       "      <td>18</td>\n",
       "      <td>8</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>310</th>\n",
       "      <td>2.3695</td>\n",
       "      <td>2.2034</td>\n",
       "      <td>3</td>\n",
       "      <td>17</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>658</th>\n",
       "      <td>2.3236</td>\n",
       "      <td>2.1768</td>\n",
       "      <td>66</td>\n",
       "      <td>19</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1368</th>\n",
       "      <td>2.2613</td>\n",
       "      <td>2.3392</td>\n",
       "      <td>73</td>\n",
       "      <td>16</td>\n",
       "      <td>4</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1627</th>\n",
       "      <td>2.3331</td>\n",
       "      <td>2.0011</td>\n",
       "      <td>66</td>\n",
       "      <td>21</td>\n",
       "      <td>7</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           x       y  accuracy  hour  day  weekday\n",
       "163   2.1663  2.3755        84    18    8        3\n",
       "310   2.3695  2.2034         3    17    3        5\n",
       "658   2.3236  2.1768        66    19    6        1\n",
       "1368  2.2613  2.3392        73    16    4        6\n",
       "1627  2.3331  2.0011        66    21    7        2"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "163     3869813743\n",
       "310     2636621520\n",
       "658     7877745055\n",
       "1368    9775192577\n",
       "1627    6731326909\n",
       "Name: place_id, dtype: int64"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2.5 分割数据集\n",
    "x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=2, test_size=0.25)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "      <th>accuracy</th>\n",
       "      <th>hour</th>\n",
       "      <th>day</th>\n",
       "      <th>weekday</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>19509166</th>\n",
       "      <td>2.3217</td>\n",
       "      <td>2.2029</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>8</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20577315</th>\n",
       "      <td>2.4800</td>\n",
       "      <td>2.2129</td>\n",
       "      <td>175</td>\n",
       "      <td>20</td>\n",
       "      <td>7</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24044078</th>\n",
       "      <td>2.2389</td>\n",
       "      <td>2.3447</td>\n",
       "      <td>79</td>\n",
       "      <td>3</td>\n",
       "      <td>8</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11279021</th>\n",
       "      <td>2.0850</td>\n",
       "      <td>2.2789</td>\n",
       "      <td>119</td>\n",
       "      <td>1</td>\n",
       "      <td>9</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19491154</th>\n",
       "      <td>2.2408</td>\n",
       "      <td>2.0092</td>\n",
       "      <td>168</td>\n",
       "      <td>0</td>\n",
       "      <td>8</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               x       y  accuracy  hour  day  weekday\n",
       "19509166  2.3217  2.2029         1     0    8        3\n",
       "20577315  2.4800  2.2129       175    20    7        2\n",
       "24044078  2.2389  2.3447        79     3    8        3\n",
       "11279021  2.0850  2.2789       119     1    9        4\n",
       "19491154  2.2408  2.0092       168     0    8        3"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3.特征工程 -- 特征预处理(标准化)\n",
    "transfer = StandardScaler()\n",
    "x_train = transfer.fit_transform(x_train)\n",
    "x_test = transfer.fit_transform(x_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/sherwin/workspaces/ai/lib/python3.6/site-packages/sklearn/model_selection/_split.py:605: Warning: The least populated class in y has only 1 members, which is too few. The minimum number of members in any class cannot be less than n_splits=10.\n",
      "  % (min_groups, self.n_splits)), Warning)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "GridSearchCV(cv=10, error_score='raise',\n",
       "       estimator=KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',\n",
       "           metric_params=None, n_jobs=1, n_neighbors=5, p=2,\n",
       "           weights='uniform'),\n",
       "       fit_params=None, iid=True, n_jobs=4,\n",
       "       param_grid={'n_neighbors': [3, 5, 7, 9]}, pre_dispatch='2*n_jobs',\n",
       "       refit=True, return_train_score='warn', scoring=None, verbose=0)"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 4.机器学习 -- knn+cv\n",
    "# 4.1 实例化一个训练器\n",
    "estimator = KNeighborsClassifier()\n",
    "\n",
    "# 4.2 交叉验证,网格搜索实现\n",
    "param_grid = {\"n_neighbors\": [3, 5, 7, 9]}\n",
    "estimator = GridSearchCV(estimator=estimator, param_grid=param_grid, cv=10, n_jobs=4)\n",
    "\n",
    "# 4.3 模型训练\n",
    "estimator.fit(x_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "准确率为:\n",
      " 0.3697158697158697\n"
     ]
    }
   ],
   "source": [
    "# 5.模型评估\n",
    "# 5.1 准确率输出\n",
    "score_ret = estimator.score(x_test, y_test)\n",
    "print(\"准确率为:\\n\", score_ret)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "预测值是:\n",
      " [3105513359 8980163153 1247398579 ... 1891783132 8169595806 3661555534]\n"
     ]
    }
   ],
   "source": [
    "# 5.2 预测结果\n",
    "y_pre = estimator.predict(x_test)\n",
    "print(\"预测值是:\\n\", y_pre)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最好的模型是:\n",
      " KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',\n",
      "           metric_params=None, n_jobs=1, n_neighbors=5, p=2,\n",
      "           weights='uniform')\n",
      "最好的结果是:\n",
      " 0.3621506121506122\n",
      "所有的结果是:\n",
      " {'mean_fit_time': array([0.07974284, 0.06533892, 0.06671908, 0.06837542]), 'std_fit_time': array([0.00468629, 0.00599286, 0.00193125, 0.00476899]), 'mean_score_time': array([0.28204682, 0.27073274, 0.30490429, 0.31999531]), 'std_score_time': array([0.01271002, 0.03878049, 0.03855714, 0.06601039]), 'param_n_neighbors': masked_array(data=[3, 5, 7, 9],\n",
      "             mask=[False, False, False, False],\n",
      "       fill_value='?',\n",
      "            dtype=object), 'params': [{'n_neighbors': 3}, {'n_neighbors': 5}, {'n_neighbors': 7}, {'n_neighbors': 9}], 'split0_test_score': array([0.32790657, 0.34330207, 0.34383295, 0.33781632]), 'split1_test_score': array([0.32915473, 0.34294413, 0.34043696, 0.3357808 ]), 'split2_test_score': array([0.3426484 , 0.3483105 , 0.34703196, 0.34191781]), 'split3_test_score': array([0.34181002, 0.35508601, 0.3539641 , 0.34835453]), 'split4_test_score': array([0.35277192, 0.36524074, 0.36581623, 0.35756762]), 'split5_test_score': array([0.35208783, 0.36522251, 0.36267399, 0.36267399]), 'split6_test_score': array([0.36639393, 0.37457593, 0.3757733 , 0.37557374]), 'split7_test_score': array([0.36424513, 0.37520292, 0.37784091, 0.37581169]), 'split8_test_score': array([0.36011537, 0.37350639, 0.37515451, 0.3710342 ]), 'split9_test_score': array([0.36964248, 0.38595024, 0.3790508 , 0.3759147 ]), 'mean_test_score': array([0.3499076 , 0.36215061, 0.36138061, 0.35739586]), 'std_test_score': array([0.01427792, 0.01416918, 0.01422346, 0.01553481]), 'rank_test_score': array([4, 1, 2, 3], dtype=int32), 'split0_train_score': array([0.61343068, 0.54832927, 0.51104823, 0.48206147]), 'split1_train_score': array([0.61508929, 0.54995255, 0.51332931, 0.48386679]), 'split2_train_score': array([0.61463646, 0.55066383, 0.51165193, 0.48258128]), 'split3_train_score': array([0.61317597, 0.55079399, 0.51032189, 0.48171674]), 'split4_train_score': array([0.61292393, 0.5488392 , 0.50936129, 0.48178025]), 'split5_train_score': array([0.61429761, 0.54829552, 0.50957372, 0.48114073]), 'split6_train_score': array([0.60945523, 0.54611501, 0.50710527, 0.48004772]), 'split7_train_score': array([0.61146321, 0.54674607, 0.50857082, 0.48092301]), 'split8_train_score': array([0.60958933, 0.54503758, 0.50596679, 0.48008239]), 'split9_train_score': array([0.61055868, 0.5450652 , 0.50791901, 0.48018658]), 'mean_train_score': array([0.61246204, 0.54798382, 0.50948483, 0.4814387 ]), 'std_train_score': array([0.00196405, 0.00205248, 0.00209691, 0.00116103])}\n"
     ]
    }
   ],
   "source": [
    "# 5.3 其他结果输出\n",
    "print(\"最好的模型是:\\n\", estimator.best_estimator_)\n",
    "print(\"最好的结果是:\\n\", estimator.best_score_)\n",
    "print(\"所有的结果是:\\n\", estimator.cv_results_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
